package com.yongcheon.sms_manager.activities;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.yongcheon.sms_manager.data.sms_spam.SMSData;
import com.yongcheon.sms_manager.data.sms_spam.SMS_Spam_Data;
import com.yongcheon.sms_manager.data.sms_spam.SpamData;
import com.yongcheon.sms_manager.receiver.sms.SmsReceiver;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;
import com.yongcheon.sms_manager.vo.SMSVO;

/**
 * Created by yongcheon on 2014-12-16.
 */
public class DeleteNotifationActivity extends Activity {
    public static final int REQ_DELETE = 123;
    SMSVO smsvo;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        String addr = bundle.getString(MainActivity.EXTRA_SPAM_ADDR);
//        String id = bundle.getString(MainActivity.EXTRA_SPAM_ID);
        String spam_category = bundle.getString(MainActivity.EXTRA_SPAM_CATEGORY);
        String spam_subject = bundle.getString(MainActivity.EXTRA_SPAM_SUBJECT);

        smsvo = new SMSVO();
        smsvo.setAddress(addr);
//        smsvo.set_Id(Long.getLong(id));
        smsvo.setSpam_category(spam_category);
        smsvo.setSpam_desc(spam_subject);

        if(DefaultAppUtil.checkIsDefault(this)){
            SMS_Spam_Data.deleteSMS_insertSpamAll(smsvo, this, new SMSData(), new SpamData(this));
            startActivity(new Intent(this, MainActivity.class));
            deleteNoti();
        }else{
            DefaultAppUtil.setDefaultAppSHow(this, DefaultAppUtil.REQ_FLAG_RESULT, REQ_DELETE, this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQ_DELETE){
            if(resultCode == RESULT_OK){
                SMS_Spam_Data.deleteSMS_insertSpamAll(smsvo, this, new SMSData(), new SpamData(this));
                startActivity(new Intent(this, MainActivity.class));
                deleteNoti();
            }else if(resultCode == RESULT_CANCELED){
                deleteNoti();
            }
        }
    }

    public void deleteNoti(){
        NotificationManager myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        myNotificationManager.cancel(SmsReceiver.NOTI_ID);
        this.finish();
    }

}